package com.qf.commons.web.aspect;

import com.qf.commons.web.utils.HttpUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;

import javax.servlet.http.HttpServletRequest;

/**
 * description: 日志增强的切面
 * author: Ken
 * 公众号：Java架构栈
 */
@Aspect
@Order(Integer.MIN_VALUE)
public class LogAdviceAspect {

    /**
     * 环绕增强
     * @return
     */
    @Around("@within(org.springframework.web.bind.annotation.RestController)")
    public Object logAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        //获取当前的客户端ip
        HttpServletRequest request = HttpUtils.getRequest();
        //客户端的ip地址
        String remoteAddr = HttpUtils.getRemoteHost(request);
        //放入MDC中
        MDC.put("ip", remoteAddr);

        Object result = joinPoint.proceed();
        return result;
    }
}
